package cn.ghf.user.controller;

import cn.ghf.basic.exception.MyException;
import cn.ghf.basic.util.AjaxResult;
import cn.ghf.user.dto.LoginDto;
import cn.ghf.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/acount")
    public AjaxResult acount(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.acount(loginDto);
            return AjaxResult.createAjaxResult().setResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }

    /**
     * 微信登录
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> map){
        try {
            return loginService.wechat(map);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }

    /**
     * 微信登录绑定手机号
     * @param map
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> map){
        try {
            Map<String,Object> loginInfo = loginService.binder(map);
            return AjaxResult.createAjaxResult().setResultObj(loginInfo);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createAjaxResult().setMsg("系统错误");
        }
    }
}
